--
-- Author: shunguo.chen
-- Date: 2018-02-06 11:00:56
--
-- WorldMapModel

local MapInfo = import(".MapInfo")
local ZoneInfo = import(".ZoneInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
	self.super.ctor(self, data)
end

function ClassRef:init()
	self._mapInfos = nil
	self._zoneDict = nil
	self._blessTimes = 0

	self:getMapInfos()
end

function ClassRef:getMapInfos()
	if not self._mapInfos then
		-- map
		local infos = {}
		local infoDict = {}
		local zoneDict = {}
		local mapConfs = GD:getWorldConfs()
		for i,conf in ipairs(mapConfs) do
			if conf.transmitType == "1" then
				local mapId = tonumber(conf.ID)
				local mapInfo = MapInfo.new(conf)
				infos[i] = mapInfo
				infoDict[mapId] = mapInfo
			end
		end

		-- zone
		local zoneConfs = GD:queryWorldZoneConfs()
		for i,conf in ipairs(zoneConfs) do
			local mapId = tonumber(conf.map)
			if mapId and infoDict[mapId] then
				local zoneInfo = ZoneInfo.new(conf)
				infoDict[mapId]:addZone(zoneInfo)
				zoneDict[zoneInfo.id] = zoneInfo
			end
		end

		local function sortFunc(a, b)
			return a.id < b.id
		end

		-- sort zones
		for i,info in ipairs(infos) do
			local zones = info:getZones()
			if #zones > 1 then
				table.sort(zones, sortFunc)
			end
		end

		self._mapInfos = infos
		self._zoneDict = zoneDict

		-- self._mapInfos[#self._mapInfos + 1] = MapInfo.new(109)
		-- self._mapInfos[#self._mapInfos + 1] = MapInfo.new(108)
	end
	return self._mapInfos
end

-- @param mapId number 地图id
-- @return MapInfo
function ClassRef:getMapInfo(mapId)
	local infos = self:getMapInfos()
	for _,info in ipairs(infos) do
		if info.id == mapId then
			return info
		end
	end
end

-- 祝福次数
function ClassRef:getBlessTimes()
	return self._blessTimes
end

function ClassRef:_setBlessTimes(times)
	self._blessTimes = times
	UD.redPoint:setStateByKey(Constant.FUNC_TYPE_DAILY_WISH_WORLD, self._blessTimes > 0)
end

-- 祝福奖励
function ClassRef:getBlessRewards(zoneId)
	local zoneInfo = self._zoneDict[zoneId]
	local fame = zoneInfo:getFame()
	local rewards = table.clone(self._blessRewards)
	table.insert(rewards, 2, fame)
	return rewards
end

-- 清空祝福奖励
function ClassRef:clearBlessRewards()
	self._blessRewards = nil
end

----------------------------------------- request ------------------------------------
-- 获取城镇信息列表
function ClassRef:reqWorldBlessInfo(cb)
	-- for test
	-- local msg = {
	-- 	infos = {
	-- 		{id=102101, prosperity=50},
	-- 		{id=102102, prosperity=50},
	-- 		{id=102103, prosperity=50},
	-- 		{id=103101, prosperity=50},
	-- 		{id=103102, prosperity=50},
	-- 		{id=103103, prosperity=50},
	-- 		{id=104101, prosperity=50},
	-- 		{id=104102, prosperity=50},
	-- 		{id=104103, prosperity=50},
	-- 		{id=105101, prosperity=50},
	-- 		{id=105102, prosperity=50},
	-- 		{id=105103, prosperity=50},
	-- 		{id=106101, prosperity=50},
	-- 		{id=106102, prosperity=50},
	-- 		{id=106103, prosperity=50}
	-- 	},
	-- 	myTimes = 1,
	-- 	expAndItemReward = {
	-- 		{type="exp", id=0, num=50},
	-- 		{type="item", id=7164603, num=1}
	-- 	}
	-- }

	-- self._blessTimes = msg.myTimes
	-- for i,info in ipairs(msg.infos) do
	-- 	local zone = self._zoneDict[info.id]
	-- 	zone:setBlessExp(info.prosperity)
	-- end

	-- -- 每天只有一份奖励，不需要每次都解析
	-- if not self._blessRewards then
	-- 	local rewards = {}
	-- 	for _,v in ipairs(msg.expAndItemReward) do
	-- 		local reward = UD:parseLootToBagItem(v)
	-- 		if reward then
	-- 			rewards[#rewards + 1] = reward
	-- 		end
	-- 	end
	-- 	self._blessRewards = rewards
	-- end

	-- executeFunc(cb)
	-- do return end

	app.session:request("pb_town.C2S_TownList",{},function(msg)
		-- dump(msg, "pb_town.C2S_TownList")
		self:_setBlessTimes(msg.myTimes)

		for i,info in ipairs(msg.infos) do
			local zone = self._zoneDict[info.id]
			zone:setBlessExp(info.prosperity)
		end

		-- 每天只有一份奖励，不需要每次都解析
		if not self._blessRewards then
			local rewards = {}
			for _,v in ipairs(msg.expAndItemReward) do
				local reward = UD:parseLootToBagItem(v)
				if reward then
					rewards[#rewards + 1] = reward
				end
			end
			self._blessRewards = rewards
		end

        if cb then
		    executeFunc(cb)
        end
	end)
end

-- 祝福
function ClassRef:reqBlessZone(id)
	-- test
	-- self._blessTimes = 0
	-- local zone = self._zoneDict[id]
	-- zone:setBlessExp(100)
	-- app:sendMsg("WorldBlessController", "onBlessZone", zone, self._blessTimes)
	-- do return end

	app.session:request("pb_town.C2S_BlessTown",{id=id},function(msg)
		-- dump(msg, "pb_town.C2S_BlessTown")
		self:_setBlessTimes(msg.myTimes)
		local zone = self._zoneDict[id]
		zone:setBlessExp(msg.info.prosperity)

		app:sendMsg("WorldBlessController", "onBlessZone", zone, self._blessTimes)

		--祝福世界移植到"日常"里了 yao.chen
		app:sendMsg("DailyController", "onBlessZone", zone, self._blessTimes)
	end)
end

return ClassRef